package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.Banner;
import com.zpp.wedding_admin.entity.vo.BannerQuery;
import com.zpp.wedding_admin.entity.vo.IDs;
import com.zpp.wedding_admin.service.IBannerService;
import com.zpp.wedding_admin.utils.LQWUtils;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(value = "轮播图管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;

    @ApiOperation(value = "获取所有轮播图")
    @GetMapping("/getAll")
    public R getAll() {
        List<Banner> bannerList = bannerService.list(null);
        return R.ok().data("bannerList", bannerList);
    }

    @ApiOperation(value = "获取前五个轮播图")
    @GetMapping("getBannerBySort")
    public R getBannerBySort() {
        LambdaQueryWrapper<Banner> lqw = LQWUtils.getLqw(Banner.class);
        lqw.orderByDesc(Banner::getLevel).last("limit 0, 5");
        List<Banner> bannerList = bannerService.list(lqw);
        return R.ok().data("bannerList", bannerList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getBannerPageVo/{currentPage}/{size}")
    public R getBannerPageVo(@PathVariable Long currentPage,
                         @PathVariable Long size) {
        Page<Banner> page = new Page<Banner>(currentPage, size);
        bannerService.page(page, null);
        List<Banner> bannerList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("bannerList", bannerList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询轮播图列表")
    @PostMapping("getBannerPageQuery/{currentPage}/{size}")
    public R getBannerPageQuery(@PathVariable Long currentPage,
                           @PathVariable Long size,
                           @RequestBody BannerQuery bannerQuery) {
        // RequestBody将json转换为实体类
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<Banner>();
        String bannerName = bannerQuery.getBannerName();
        lqw.like(!StringUtils.isEmpty(bannerName), Banner::getBannerName, bannerName);
        Page<Banner> page = new Page<Banner>(currentPage, size);
        bannerService.page(page, lqw);
        List<Banner> bannerList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("bannerList", bannerList).data("total", total);
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/deleteAllSelect")
    public R deleteAllSelect(@RequestBody Integer iDs[]) {
        boolean b = bannerService.deleteAllSelect(iDs);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id删除一个")
    @DeleteMapping("/deleteOneById/{id}")
    public R deleteOneById(@PathVariable Integer id) {
        boolean b = bannerService.removeById(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id获取轮播图信息")
    @GetMapping("/getOneById/{id}")
    public R getOneById(@PathVariable Integer id) {
        Banner banner = bannerService.getById(id);
        if ( banner != null ) return R.ok().data("banner", banner);
        return R.error();
    }

    @ApiOperation(value = "更新或添加")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody Banner banner) {
        boolean b = bannerService.saveOrUpdate(banner);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "模糊查询名称")
    @GetMapping("/searchByName/{bannerName}")
    public R searchByName(@PathVariable String bannerName) {
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<Banner>();
        lqw.like(Banner::getBannerName, bannerName);
        List<Banner> bannerList = bannerService.list(lqw);
        return R.ok().data("bannerList", bannerList);
    }
}
